<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [
<!ENTITY tab "<xsl:text>    </xsl:text>">
<!ENTITY tab2 "<xsl:text>       </xsl:text>">
<!ENTITY tab3 "<xsl:text>            </xsl:text>">
<!ENTITY cr "<xsl:text>
</xsl:text>">
<!ENTITY space "<xsl:text> </xsl:text>">
]>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"
    xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml" indent="yes"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
    
    <xsl:key name="inputCurrencyID" match="report/exchanges/exchange/input_currency" use="."/>
    <xsl:key name="outputCurrencyID" match="report/exchanges/exchange/output_currency" use="."/>

<xsl:template match="/">
	<html >
	<head>&cr;
        &tab;<title>Report</title>&cr;
	&tab;<link rel="stylesheet" type="text/css" href="style.css" />&cr;
	</head>
	<body>
            <xsl:call-template name="info" />
            <xsl:call-template name="exchanges" />
     		&tab;<div class="reportname">Statistics:</div>&cr;
            &tab;<div class="statistics">
            <xsl:call-template name="count_exchanges" />
            <xsl:call-template name="input_currency_stats" />
            <xsl:call-template name="output_currency_stats" />
            &tab;</div>
	</body>
	</html>
</xsl:template>
    
<xsl:template name="info">
        <p id="mainTitle">Modern Content Management Technologies</p>&cr;
        &tab;<div id="info">&cr;
            &tab2;<div id="authors">Authors:</div>&cr;
            <xsl:for-each select="//authors">
                &tab2;<div class="author">&cr;
                <xsl:for-each select="author">
                        &tab3;<xsl:value-of select="name" />&space;-&space;
                        <xsl:value-of select="index_number" />&space;-&space;
                        <xsl:value-of select="department" />&cr;
                    &tab3;<br />&cr;
                </xsl:for-each>
                &tab2;</div>&cr;
            </xsl:for-each>
        &tab;</div>&cr;
    </xsl:template>
    
    <xsl:template name="exchanges">
     	<div class="reportname">List of exchanges:</div>&cr;
            &tab;<table class="report">&cr;
            &tab2;<tr class="items">&cr;
                &tab3;<td class="header">Input Value</td>&cr;
                &tab3;<td class="header">Input Currency</td>&cr;
                &tab3;<td class="header">Output Value</td>&cr;
                &tab3;<td class="header">Output Currency</td>&cr;
                &tab3;<td class="header">Rate</td>&cr;
                &tab3;<td class="header">Title</td>&cr;
                &tab3;<td class="header">Converter source link</td>&cr;
                &tab3;<td class="header">Region</td>&cr;
                &tab3;<td class="header">Publish date</td>&cr;
                &tab3;<td class="header">Description</td>&cr;
            &tab2;</tr>&cr;
     <xsl:for-each select="//exchange">
            &tab2;<tr class="items">&cr;
            	&tab3;<td class="item"><xsl:value-of select="input_value" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="input_currency" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="output_value" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="output_currency" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="rate" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="title" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="link" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="region" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="pubdate" /></td>&cr;
            	&tab3;<td class="item"><xsl:value-of select="description" /></td>&cr;
            &tab2;</tr>&cr;
	</xsl:for-each>
    	&tab;</table>&cr;
</xsl:template>

    <xsl:template name="input_currency_stats">
        &tab;&tab;<div class="statistic" >&cr;
        	<div class="statsTitle">Input Currency in percentages: </div>&cr;
            <xsl:for-each select="report/exchanges//input_currency[generate-id() = generate-id(key('inputCurrencyID',.)[1])]">
                <xsl:sort select="count(key('inputCurrencyID',.))" order="descending"/>
                &tab3;<div class="entry" >&cr;
                    &tab3;&tab;<div>
                    &tab3;&tab;<xsl:value-of select="." /> - &cr;
                    &tab3;&tab;<xsl:value-of select="round((count(key('inputCurrencyID',.))  div (count(//exchange)) * 100))" />
                    &tab3;&tab;</div>&cr;
                &tab3;</div>&cr;           
            </xsl:for-each>
            &tab;&tab;</div>&cr;
    </xsl:template>

    <xsl:template name="output_currency_stats">
        &tab2;<div class="statistic" >&cr;
        	<div class="statsTitle">Output Currency in percentages: </div>&cr;
            <xsl:for-each select="report/exchanges//output_currency[generate-id() = generate-id(key('outputCurrencyID',.)[1])]">
                <xsl:sort select="count(key('outputCurrencyID',.))" order="descending"/>
                &tab3;<div class="entry" >&cr;
                    &tab3;&tab;<div>
                    &tab3;&tab;<xsl:value-of select="." /> - &cr;
                    &tab3;&tab;<xsl:value-of select="round((count(key('outputCurrencyID',.))  div (count(//exchange)) * 100))" />
                    &tab3;&tab;</div>&cr;
                &tab3;</div>&cr;           
            </xsl:for-each>
            &tab;&tab;</div>&cr;
    </xsl:template>
    
    <xsl:template name="count_exchanges">
        &tab;&tab;<div class="statistic" >&cr;
        	<div class="statsTitle">Total number of performed exchanges: </div>&cr;
            &tab3;<div class="entry">&cr;
                &tab3;&tab;<div><xsl:value-of select="count(//exchange)" /></div>&cr;
            &tab3;</div>&cr; 
            &tab;&tab;</div>&cr;
    </xsl:template>
</xsl:stylesheet>